﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Security;
using DAO;
using BUS;
using DTO;
using System.Drawing;
using System.Collections;

namespace TuyenDungViecLam.Admin
{
    public partial class CreateUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 IEnumerable dsRole =User_BUS.GetAllRole();
                 
                    
                 CblAddRoleForUser.DataSource = dsRole;
                 CblAddRoleForUser.DataTextField = "Name";
                 CblAddRoleForUser.DataValueField = "RoleID";
                 CblAddRoleForUser.DataBind();
               
            }
        }

        protected void BtCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsUserAvailable(txtUsername.Text))
                {
                    User_DTO newUser = new User_DTO();
                    newUser.UserName = txtUsername.Text;
                    newUser.PassWord = txtPassword.Text;
                    newUser.Date = DateTime.Now.Date;
                    newUser.Email = txtEmail.Text;

                    if (CbActiveUser.Checked)
                        newUser.Status = 1;
                    else
                        newUser.Status = 0;

                    int kiemTraCoChonRole = 0;

                    if (CblAddRoleForUser.SelectedItem != null)
                    {
                        kiemTraCoChonRole = 1;
                    }
                    else
                    {
                        kiemTraCoChonRole = 0;
                    }

                    int kiemTRaLaNguoiTimViec = 0;  //0:nguoi tim viec , 1:nha tuyen dung
                    if (RbNguoiTimViec.Checked)
                    {
                        kiemTRaLaNguoiTimViec = 0;
                    }
                    else
                        kiemTRaLaNguoiTimViec = 1;

                    bool kq = false;
                    for (int i = 0; i < CblAddRoleForUser.Items.Count; i++)
                    {
                        if (CblAddRoleForUser.Items[i].Selected)
                        {
                            kq = User_BUS.CreateUser(newUser, kiemTraCoChonRole, kiemTRaLaNguoiTimViec, int.Parse(CblAddRoleForUser.Items[i].Value));

                            if (kq == false)
                                break;
                            else
                                kq = true;
                        }
                    }

                    //bool kq = User_BUS.CreateUser(newUser,kiemTraCoChonRole,);

                    if (kq == true)
                    {
                        divCapnhat.Visible = false;
                        divThongbao.Visible = true;
                        lbThongbao.Text = "Tạo User thành công";
                        lbThongbao.ForeColor = Color.Blue;
                    }
                    else
                    {
                        divThongbao.Visible = true;
                        lbThongbao.Text = "Tạo User thất bại";
                        lbThongbao.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblMessage.Text = "Username này đã tồn tại";
                }
            }
            catch (Exception)
            {
                divThongbao.Visible = true;
                lbThongbao.Text = "Tạo User thất bại";
                lbThongbao.ForeColor = Color.Red;
            }
        }

        [WebMethod]
        public static bool IsUserAvailable(string username)
        {
            MyMembershipProvider myMembership = new MyMembershipProvider();

            bool kq = myMembership.GetUser(username);

            if (kq == true)
            {
                return false;
            }
            else
            {
                return true;
            }


        }

       
    }
}
